package DBPool;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

/**
 * Created by sunwei on 2017/9/27
 * Time:9:02
 */
public class DBConfig {
    public static int MAX_POOL_SIZE = 20;//连接池里面最大连接数量
    public static String driver;//数据库驱动
    public static String URL;//数据库的地址
    public static String userName;//数据库的名字
    public static String password;//数据库的密码
    public static boolean AUTOCOMMIT=true;//是否自动提交，true时为允许自动提交
    static {
        loadConfig();
//        ConfigInformation();
    }
    //第一种方式加载配置信息
    public static void loadConfig(){
        InputStream inputStream = DBConfig.class.getResourceAsStream("/config_db.properties");
        Properties properties = new Properties();
        try {
            properties.load(inputStream);
            driver= (String) properties.getProperty("driver");
            userName = (String) properties.getProperty("username");
            password = (String)properties.getProperty("password");
            URL = (String) properties.getProperty("url");
            MAX_POOL_SIZE=Integer.parseInt(properties.getProperty("poolsize"));
            AUTOCOMMIT=Boolean.parseBoolean(properties.getProperty("autocommit"));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取配置信息异常！");
        }
    }
    //第二种方式加载配置信息
    public static void ConfigInformation(){
        try {
            Properties properties = new Properties();//实例化对象，来读取配置信息
            String path = DBConfig.class.getResource("/").getPath();//获取所要读取的properties文件的路径
            properties.load(new FileInputStream(path+"config_db.properties"));//根据路径，来读取文件，转化为字节流
            URL = (String)properties.getProperty("url");
            driver = (String)properties.getProperty("driver");
            userName = (String)properties.getProperty("username");
            password = (String)properties.getProperty("password");
            MAX_POOL_SIZE =  Integer.parseInt(properties.getProperty("poolsize"));
            AUTOCOMMIT = Boolean.parseBoolean(properties.getProperty("autocommit"));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }



}
